package com.example.web;

import com.example.pojo.Book;
import com.example.pojo.Page;
import com.example.service.BookService;
import com.example.service.impl.BookServiceImpl;
import com.example.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo=WebUtils.parseInt(request.getParameter("pageNo"),0);
//        pageNo++;
        //1.获取请求的参数，封装成book对象
//        System.out.println(request.getParameterMap());
        Book book = WebUtils.copyParamToBean(request.getParameterMap(), new Book());

        //2.调用BookService.addBook()保存图书
        bookService.addBook(book);

        //3.跳到图书列表首页
        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=page&pageNo="+pageNo);

    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.获取请求的参数id，图书编号
        int id = WebUtils.parseInt(request.getParameter("id"), -1);
        //2.调用BooService.deleteBookById();删除图书
        bookService.deleteBookById(id);
        //3.重定向回图书列表管理页面
        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=page&pageNo="+request.getParameter("pageNo"));
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Book book = WebUtils.copyParamToBean(request.getParameterMap(), new Book());

        bookService.updateBook(book);

        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=page&pageNo="+request.getParameter("pageNo"));

    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.通过BookService查询全部图书
        List<Book> books = bookService.queryBooks();
        //2.把全部图书保存到Request域中
        request.setAttribute("books", books);
        //3.请求转发到/pages/manager/book_manager.jsp页面
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);


    }

    protected void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.获取请求的参数图书编号
        int id = WebUtils.parseInt(request.getParameter("id"), -1);
        //2.查询图书
        Book book = bookService.queryBookById(id);
        //3.保存图书到request
        request.setAttribute("book", book);
        //4.转发到jsp页面
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request, response);
    }

    /**
     * 处理分页功能
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.首先获取请求的参数 pageNo 和 pageSize
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);

        //2.调用BookService.page(pageNo,pageSize) : page对象
        Page<Book> page = bookService.page(pageNo, pageSize);

        page.setUrl("manager/bookServlet?action=page");
        //3.保存page对象到request中
        request.setAttribute("page", page);
        //4.请求转发到book_manager.jsp页面
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
    }
}
